// pages/center/index/index.js
const app = getApp();
const service = require('../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否登录了
    isLogin: false,
    menuList: [{
      icon: '/images/center/5.png',
      title: '优惠券',
      url: 'pages/center/coupon/coupon'
    }, {
      icon: '/images/center/4.png',
      title: '发票',
      url: 'pages/center/invoice/index/index'
    }, {
      icon: '/images/center/14.png',
      title: '我的订单',
      url: 'pages/order/index/index'
    }, {
      icon: '/images/center/6.png',
      title: '联系人管理',
      url: 'pages/center/liaison/liaison'
    }, {
      icon: '/images/center/7.png',
      title: '关于我们',
      url: 'pages/center/aboutUs/aboutUs'
    }, {
      icon: '/images/center/13.png',
      title: '联系客服',
      url: 'pages/customerService/index/index'
    }],
    // 是否是司机
    userType: 0,
    // 司机菜单
    driverMenu: {
      icon: '/images/center/8.png',
      title: '司机中心',
      url: 'pages/driver/index/index'
    },

    // 核销员菜单
    verifierMenu: {
      icon: '/images/center/10.png',
      title: '核销中心',
      url: 'pages/verifier/index/index'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loginDialog = this.selectComponent("#login-dialog");
    options.pageName && wx.setNavigationBarTitle({
      title: options.pageName,
    });
  },

  // 点击登录注册
  bindLogin() {
    this.loginDialog.showLoginDialog();
  },

  // 跳转我的二维码
  bindToMyQrCode() {
    wx.navigateTo({
      url: '/pages/center/myShare/index?qrCode=' + this.data.userInfo.shareQr,
    })
  },

  /**
   * 加载个人信息
   */
  loadData() {
    var that = this;
    service.getConnect(service.api.getPersonalInfo).then(function (res) {
      let temp = Object.assign({}, {
        userInfo: res.data,
        userType: parseInt(res.data.userType),
        isLogin: true
      });
      wx.hideLoading();
      that.setData(temp);
    })
  },

  // 跳转页面
  bindJumpToPage(e) {
    var page = e.currentTarget.dataset.page;
    app.jumpToPage('/' + page.url, page.name);
  },

  // 退出登录
  bindToLogout() {
    var that = this;
    app.showConfirm('提示', '是否要退出登录?', true, function(){
      service.getConnect(service.api.logout).then(function (res) {
        wx.showToast({
          title: res.msg,
        })
        wx.setStorageSync("userInfo", '');
        app.globalData.userInfo = '';
        wx.setStorageSync("token", '');
        app.globalData.token = '';
        wx.setStorageSync("sharerId", '');
        wx.reLaunch({
          url: '/pages/index/index',
        })
        wx.hideLoading();
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (app.globalData.userInfo && app.globalData.userInfo.numberId) {
      this.loadData();
    }else{
      this.setData({
        isLogin: false,
        userInfo: {}
      })
    }
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3,
        selectedColor: app.globalData.config['selectedColor'],
        list: app.globalData.config.tarbarList
      })
    }
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.loginDialog.hideLoginDialog();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (app.globalData.userInfo && app.globalData.userInfo.numberId) {
      this.loadData();
    } else {
      this.setData({
        isLogin: false,
        userInfo: {}
      })
    }
    app.utils.initWxappStyle(app, this);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }
  },

  // 调试模式
  clickDebugCount: 0,
  bindToDebug(){
    this.clickDebugCount++;
    if(this.clickDebugCount===10){
      this.clickDebugCount = 0;
      wx.navigateTo({
        url: '/pages/_debug/index',
      })
    }
  }
})
